//
//  XKRegisterViewController.m
//  xk100
//
//  Created by xiaokang on 2017/6/6.
//  Copyright © 2017年 xiaokang. All rights reserved.
//

#import "XKRegisterViewController.h"
#import "XKRegisterView.h"
#import "RewardSuccess.h"

@interface XKRegisterViewController () <XKRegisterViewDelegate>
@property (nonatomic, strong) XKRegisterView *registerView;
@end

@implementation XKRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSubviews];
    [self isShouldCountdown];
}

- (void)initSubviews {
    self.navigationItem.title = @"快速注册";
    
    _registerView = [[XKRegisterView alloc] initWithFrame:self.view.bounds];
    _registerView.delegate = self;
    _registerView.phoneString = _phoneString;
    [self.view addSubview:_registerView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self cancelCountdown];
}

- (void)registerWithNickName:(NSString *)nickName phone:(NSString *)phone vcode:(NSString *)vcode password_new:(NSString *)password_new password_confirm:(NSString *)password_confirm inviter:(NSString *)inviterPhone {
    
    if (![password_new isEqualToString:password_confirm]) {
        [SVProgressHUD showInfoWithStatus:@"两次密码输入不一致"];
        return;
    }
    
    [self.view endEditing:YES];
    [SVProgressHUD show];
    
    @weakify(self);
    [APIManager POSTRequestWith:APIPOSTRequestState_RegisterNewAccount parameters:@{@"nick":nickName, @"phone":phone, @"vcode":vcode, @"pwd":password_confirm, @"inviter_id":inviterPhone} successBlock:^(NSDictionary *successDict) {
        @strongify(self);
        [SVProgressHUD dismiss];
        [RewardSuccess show];// 提示升级成功(散花效果)
        [MobClick event:@"NewAccountRegisterSuccess"]; // 友盟事件统计
        // 延时返回登录页
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            @strongify(self);
            [self.navigationController popViewControllerAnimated:YES];
        });
        
    } failureBlock:^(NSDictionary *failureDict) {
        
    } errorBlock:^(NSError *error) {
        
    }];
}

#pragma mark - 获取验证码
- (void)getvcodeWithPhone:(NSString *)phone {
    if (![phone isMobileNumber]) {
        [SVProgressHUD showInfoWithStatus:@"手机号格式错误"];
        return;
    }
    
    [APIManager POSTRequestWith:APIPOSTRequestState_SendSMSCode parameters:@{@"phone":phone, @"vcode_type":@(1)} successBlock:^(NSDictionary *successDict) {
        [self countdownWithTimeinterval:60];
        NSLog(@"%@",[NSString jsonToString:successDict]);
    } failureBlock:^(NSDictionary *failureDict) {
    } errorBlock:^(NSError *error) {
    }];
}

#pragma mark - 开始时是否倒计时
- (void)isShouldCountdown {
    NSString *timeOut_get = [NSString calculateNumberOne:[[NSUserDefaults standardUserDefaults] objectForKey:@"timeOut_register"] calculateType:CalculateType_Subtract numberTwo:[NSString getTimestamp]];
    if (timeOut_get.integerValue > 0) {
        [self countdownWithTimeinterval:timeOut_get.integerValue];
    }
}

#pragma mark - 取消倒计时
- (void)cancelCountdown {
    [self.registerView.vcodeButton cancelCountdown];
}

#pragma mark - 倒计时
- (void)countdownWithTimeinterval:(NSInteger)timeinterval{
    // 记录结束时间
    [[NSUserDefaults standardUserDefaults] setObject:[NSString calculateNumberOne:[NSString getTimestamp] calculateType:CalculateType_Add numberTwo:[NSString stringWithFormat:@"%zd",timeinterval]] forKey:@"timeOut_registeraccount"];
    
    @weakify(self);
    [self.registerView.vcodeButton startTime:timeinterval waitBlock:^(NSInteger remainTime) {
        @strongify(self);
        self.registerView.vcodeButton.enabled = NO;
        [self.registerView.vcodeButton setTitle:[NSString stringWithFormat:@"已发送(%zds)",remainTime] forState:UIControlStateNormal];
    } finishBlock:^{
        @strongify(self);
        self.registerView.vcodeButton.enabled = YES;
        [self.registerView.vcodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    }];
}


- (void)setPhoneString:(NSString *)phoneString {
    _phoneString = phoneString;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
